﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bank.DTOs;
using bank.BusinessFacade;
using bank.DataSets;


public partial class StudentReg_StudentEntry : System.Web.UI.Page
{
    School dsSchool = new School();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["schooldata"] = dsSchool;
            PopulateStudDataWithDataSet2();
        }
        //PopulateStudData();
        // PopulateStudDataWithDataSet();
        
    }
    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        dsSchool = (School)Session["schooldata"];
        DataRow row = dsSchool.StudentDT.NewRow();
        row["RollNo"] = Int64.Parse(txtRoll.Text);
        row["Name"] = txtName.Text;
        row["Age"] = Convert.ToInt32(txtAge.Text);
        dsSchool.StudentDT.Rows.Add(row);
        Session["schooldata"] = dsSchool;
        grdStudent.DataSource = dsSchool.StudentDT;
        grdStudent.DataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        dsSchool = (School)Session["schooldata"];
        DataRow[] dr = dsSchool.StudentDT.Select("RollNo=" + txtRoll.Text);
        if (dr != null)
        {
            dr[0].Delete();
            grdStudent.DataSource = dsSchool.StudentDT;
            grdStudent.DataBind();
            Session["schooldata"] = dsSchool;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        dsSchool = (School)Session["schooldata"];
        DataRow[] dr = dsSchool.StudentDT.Select("RollNo=" + txtRoll.Text);
        if (dr != null)
        {
            dr[0]["Name"] = txtName.Text;
            dr[0]["Age"] = Convert.ToInt32(txtAge.Text);

            grdStudent.DataSource = dsSchool.StudentDT;
            grdStudent.DataBind();
            Session["schooldata"] = dsSchool;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        StudentFacade facade = new StudentFacade();
        dsSchool = (School)Session["schooldata"];
        facade.UpdateStudent(dsSchool);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //StudentInformation student = new StudentInformation();
        //student.RollNo = Int64.Parse(txtRoll.Text);
        //student.Name = txtName.Text;
        //student.Age = Convert.ToInt32(txtAge.Text);

        //Query query = new Query();
        //query.AddStudentData(student);
    }
    private void PopulateStudData()
    {
        //Query qry = new Query();
        //grdStudent.DataSource = qry.GetStudents();
        //grdStudent.DataBind();
    }
    private void PopulateStudDataWithDataSet()
    {
        //Query qry = new Query();
        //DataSet dsStud = qry.GetAllStudents();
        //grdStudent.DataSource = dsStud.Tables["StudentLocal"];
        //grdStudent.DataBind();

        //grdTec.DataSource = dsStud.Tables["TechLocal"];
        //grdTec.DataBind();

    }
    private void PopulateStudDataWithDataSet2()
    {
        StudentFacade facade = new StudentFacade();
        School dsStud = facade.GetAllStudentsByAge(21);
        grdStudent.DataSource = dsStud.StudentDT;
        grdStudent.DataBind();

        Session["schooldata"] = dsStud;
        //grdTec.DataSource = dsStud.Tables["TechLocal"];
        //grdTec.DataBind();

    }
}